package cn.com.miao.lynx.common.web.config;

import cn.com.miao.lynx.common.web.config.jackson.JacksonHttpMessageConverter;
import cn.com.miao.lynx.common.web.config.jackson.JacksonJavaTimeModule;
import cn.com.miao.lynx.common.web.config.undertow.UndertowServerFactoryCustomizer;
import cn.hutool.core.date.DatePattern;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.undertow.Undertow;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.time.ZoneId;
import java.util.Locale;
import java.util.TimeZone;

/**
 * WebMvcConfig
 * web mvc配置
 * @title: WebMvcConfig
 * @description:
 * @author: dengmiao
 * @create: 2019-12-26 17:19
 **/
@ConditionalOnWebApplication
@Configuration
public class WebMvcConfig implements ServletContextInitializer, WebMvcConfigurer {

    /*@Bean
    public HttpMessageConverters httpMessageConverters() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();

        builder.locale(Locale.CHINA);
        builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
        builder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
        builder.modules(new JacksonJavaTimeModule());

        // 不包含null
        // builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        ObjectMapper objectMapper = builder.build();
        // SimpleModule simpleModule = new SimpleModule();
        // simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        // objectMapper.registerModule(simpleModule);
        // 忽略 transient 修饰的属性
        objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
        return new HttpMessageConverters(new *//*MappingJackson2HttpMessageConverter*//*JacksonHttpMessageConverter(objectMapper));
    }*/

    @Bean
    @ConditionalOnClass(Undertow.class)
    public UndertowServerFactoryCustomizer undertowServerFactoryCustomizer() {
        return new UndertowServerFactoryCustomizer();
    }

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        /*是否允许请求带有验证信息*/
        corsConfiguration.setAllowCredentials(true);
        /*允许访问的客户端域名*/
        corsConfiguration.addAllowedOrigin("*");
        /*允许服务端访问的客户端请求头*/
        corsConfiguration.addAllowedHeader("*");
        /*允许访问的方法名,GET POST等*/
        corsConfiguration.addAllowedMethod("*");
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

    }
}
